﻿namespace EyeOpen.BomberMan.Windows
{
	using System;
	using System.Drawing;
	using System.Windows.Forms;

	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();

			var lines =
				GetType()
				.Assembly
				.GetText("Resources.Maps.Map.txt")
				.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

			var gameEnvironment =
				new GameEnvironment(
					new MapCreator().Create(lines, () => fieldPictureBox.Invalidate()),
					TemporizedMethod);

			var gameDesigner = 
				new GameDesigner(
					new ImageInitializer(GetType().Assembly, "Resources.Imgs"),
					gameEnvironment,
					new Size(40, 40));

			KeyDown += (s, e) => gameEnvironment.Action(new KeyEventToActionType().Convert(e.Control, e.KeyCode));
			fieldPictureBox.Paint += (s, e) => gameDesigner.Paint(e.Graphics);

			fieldPictureBox.Width = gameDesigner.Width;
			fieldPictureBox.Height = gameDesigner.Height;

			Width = fieldPictureBox.Width + 36;
			Height = fieldPictureBox.Height + 60;
		}

		private void TemporizedMethod(Action action)
		{
			var timer =
				new Timer
				{
					Interval = 5000
				};

			timer.Tick +=
				(s, e) =>
					{
						using (timer)
						{
							timer.Stop();
							action();
						}
					};

			timer.Start();
		}
	}
}
